/**
 * 
 * @author Huang Junjie
 * 
 */

package TimeSqueeze.logic;

import TimeSqueeze.storage.TempStorage;
import TimeSqueeze.common.*;

public class Update implements CommandInterface {

	private String descriptionOfTask;
	private String newContentToChange;
	private Time newStartTime;
	private Time newEndTime;

	private Task taskInput;
	private DeadlineTask deadlineTaskInput;
	private TimedTask timedTaskInput;

	// this is for changing untimed task description
	public Update(Task task, String paras) {
		taskInput = task;
		newContentToChange = paras;
	}

	// this is for changing untimed task tag
	public Update(Task task) {
		taskInput = task;
	}

	// this is for changing timed task description
	public Update(TimedTask task, String paras) {
		timedTaskInput = task;
		newContentToChange = paras;
	}

	// this is for changing timed task time or tag
	public Update(TimedTask task) {
		timedTaskInput = task;
	}

	// this is for changing deadline task tag or time
	public Update(DeadlineTask task) {
		deadlineTaskInput = task;
	}

	// this is for changing deadline task description
	public Update(DeadlineTask task, String paras) {
		deadlineTaskInput = task;
		newContentToChange = paras;
	}

	@Override
	public void execute() {
		if (taskInput != null) {
			if (taskInput.getTag() != null) { // need to change tag
				descriptionOfTask = taskInput.getDescription();
				newContentToChange = taskInput.getTag();
				updateUntimedTaskTag();
			} else { // need to change task description
				descriptionOfTask = taskInput.getDescription();
				updateUntimedTaskDescription();
			}
		}

		if (timedTaskInput != null) {
			if (timedTaskInput.getTag() != null) { // need to change tag
				descriptionOfTask = timedTaskInput.getDescription();
				newContentToChange = timedTaskInput.getTag();
				updateTimedTaskTag();
			} else if (timedTaskInput.getStartTime() != null) { // need to
																// change time
				descriptionOfTask = timedTaskInput.getDescription();
				newStartTime = timedTaskInput.getStartTime();
				newEndTime = timedTaskInput.getEndTime();
				updateTimedTaskTime();
			} else { // need to change task description
				descriptionOfTask = timedTaskInput.getDescription();
				updateTimedTaskDescription();
			}
		}

		if (deadlineTaskInput != null) {
			if (deadlineTaskInput.getTag() != null) { // need to change tag
				descriptionOfTask = deadlineTaskInput.getDescription();
				newContentToChange = deadlineTaskInput.getTag();
				updateDeadlineTaskTag();
			} else if (deadlineTaskInput.getEndTime() != null) { // need to
																	// change
																	// time
				descriptionOfTask = deadlineTaskInput.getDescription();
				newEndTime = deadlineTaskInput.getEndTime();
				updateDeadlineTaskTime();
			} else { // need to change task description
				descriptionOfTask = deadlineTaskInput.getDescription();
				updateDeadlineTaskDescription();
			}
		}
	}

	private void updateUntimedTaskTag() {
		for (int i = 0; i < TempStorage.untimedTasks.size(); i++) {
			if (TempStorage.untimedTasks.get(i).getDescription()
					.equals(descriptionOfTask)) {
				TempStorage.untimedTasks.get(i).setTag(newContentToChange);
			}
		}
	}

	private void updateUntimedTaskDescription() {
		for (int i = 0; i < TempStorage.untimedTasks.size(); i++) {
			if (TempStorage.untimedTasks.get(i).getDescription()
					.equals(descriptionOfTask)) {
				TempStorage.untimedTasks.get(i).setDescription(
						newContentToChange);
			}
		}
	}

	private void updateTimedTaskTag() {
		for (int i = 0; i < TempStorage.timedTasks.size(); i++) {
			if (TempStorage.timedTasks.get(i).getDescription()
					.equals(descriptionOfTask)) {
				TempStorage.timedTasks.get(i).setTag(newContentToChange);
			}
		}
	}

	private void updateTimedTaskTime() {
		for (int i = 0; i < TempStorage.timedTasks.size(); i++) {
			if (TempStorage.timedTasks.get(i).getDescription()
					.equals(descriptionOfTask)) {
				TempStorage.timedTasks.get(i).setStartTime(newStartTime);
				TempStorage.timedTasks.get(i).setEndTime(newEndTime);
			}
		}
	}

	private void updateTimedTaskDescription() {
		for (int i = 0; i < TempStorage.timedTasks.size(); i++) {
			if (TempStorage.timedTasks.get(i).getDescription()
					.equals(descriptionOfTask)) {
				TempStorage.timedTasks.get(i)
						.setDescription(newContentToChange);
			}
		}
	}

	private void updateDeadlineTaskTag() {
		for (int i = 0; i < TempStorage.deadlineTasks.size(); i++) {
			if (TempStorage.deadlineTasks.get(i).getDescription()
					.equals(descriptionOfTask)) {
				TempStorage.deadlineTasks.get(i).setTag(newContentToChange);
			}
		}
	}

	private void updateDeadlineTaskTime() {
		for (int i = 0; i < TempStorage.deadlineTasks.size(); i++) {
			if (TempStorage.deadlineTasks.get(i).getDescription()
					.equals(descriptionOfTask)) {
				TempStorage.deadlineTasks.get(i).setEndTime(newEndTime);
			}
		}
	}

	private void updateDeadlineTaskDescription() {
		for (int i = 0; i < TempStorage.deadlineTasks.size(); i++) {
			if (TempStorage.deadlineTasks.get(i).getDescription()
					.equals(descriptionOfTask)) {
				TempStorage.deadlineTasks.get(i).setDescription(
						newContentToChange);
			}
		}
	}

	@Override
	public void undo() {
	}

}

